import requests
from bs4 import BeautifulSoup
url = r'http://stock.hexun.com/2021-07-27/204034148.html'

def parse_stock_code(url):
    headers = {
        'Accept': 'application/json, text/javascript, */*; q=0.01',
        'Accept-Encoding': 'gzip, deflate, br',
        'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
        'Cache-Control': 'no-cache',
        'Connection': 'keep-alive',
        #'Host': 'www.baidu.com',
        'Pragma': 'no-cache',
        'Referer': url,
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.75 Safari/537.36'
    }
    key_info = url,headers
    try:
        resp = requests.get(url=key_info[0], headers=headers, timeout=5)
        resp.encoding = 'gbk'
        #print(resp.text)
        soup = BeautifulSoup(resp.text, 'lxml')
        data_list = []
        texts = []
        try:
            for div in soup.find_all('p'):
                texts.append(div.text)
                #print(div.text)

        except Exception as e:
            pass
        stock_code  = []
        for text in texts:
            for i ,c in enumerate(text):
                s =  text[i:i+6]
                if s.isdigit() and len(s) ==6:
                    print(s)
                    stock_code.append(s)
    except Exception as e:
        stock_code = []
        texts = ""
    return stock_code,texts

# codes , texts = parse_stock_code("https://new.qq.com/rain/a/20210724A03TQ500")
# print("提取股票代码")
# print(codes)
#
# from api import query_code
# print("获得股票名称")
# print(query_code(codes[0]).split(',')[0])
